package com.ld.common.service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ld.admin.model.system.SystemUser;
import com.ld.admin.service.user.UserService;
import com.ld.common.util.PassUtil;
import com.ld.common.util.systeminit.SystemInitModel;
import com.ld.common.util.systeminit.SystemInitUtil;
import com.ld.shieldsb.common.core.collections.ListUtils;
import com.ld.shieldsb.common.core.model.PropertiesModel;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.core.util.StringUtils;
import com.ld.shieldsb.common.web.util.Web;
import com.ld.shieldsb.dao.model.QueryModel;

/**
 * 项目中的基础service，继承自BasicService，做一些扩展
 * 
 * @ClassName ProjectBasicService
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年4月22日 下午5:24:39
 *
 */
@Service
public class SystemInitService extends ProjectBasicService {
    @Autowired
    public UserService userService;

    public Result systemInit(SystemInitModel model) {
        Result result = new Result();

        if (SystemInitUtil.needInit()) {

            // 1、修改项目配置
            String projectName = model.getProjectName(); // 项目名称
            if (StringUtils.isNotEmpty(projectName)) {
                PropertiesModel.PROJECT.save("projectName", projectName);
            }

            // 2、修改内置用户密码
            final String password = model.getPassword();
            if (PassUtil.checkPwdSimple(password)) {
                return Web.Response.error("密码过于简单，请返回修改！");
            }
            QueryModel queryModel = userService.getNormalQueryModel();
            Set<Long> innerUserIds = new HashSet<>();
            innerUserIds.add(2L); // 超级管理员
            innerUserIds.add(1L); // 系统管理员
            innerUserIds.add(13L); // 测试用户
            queryModel.addInSet(SystemUser::getId, innerUserIds);

            // 修改用户密码
            List<SystemUser> userList = userService.getList(SystemUser.class, queryModel);
            if (ListUtils.isNotEmpty(userList)) {
                for (SystemUser user : userList) {
                    user.setPassword(password);
                    Result restPwdResult = userService.resetPassword(user, password);
                    if (!restPwdResult.getSuccess()) {
                        return restPwdResult;
                    }
                }
            }

            // 3、保存配置
            PropertiesModel.CONFIG.save("security.access.authname", model.getSwaggerUserName());
            PropertiesModel.CONFIG.save("security.access.authorization", model.getSwaggerPassword());

            // 4、保存配置
//            SystemInitModel model = SystemInitUtil.readObjFromConfigFile();
            model.setInit(true); // 初始化完成
            result = SystemInitUtil.saveObj2ConfigFile(model);

            if (result.getSuccess()) {
                SystemInitUtil.saveInitState2DB(result.getSuccess());// 保存到数据库
                return Web.Response.success("初始化完成，设置系统内置用户【admin\\system\\test】密码为" + password + ",建议重启后操作！");
            }

        } else {
            result.setMessage("已经初始化过！不能重复操作");
        }

        return result;
    }

}
